<html>
<head><meta charset="utf-8"><title>HashMap::remove return value · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html">HashMap::remove return value</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="167929933"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167929933" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Papp <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167929933">(Jun 12 2019 at 09:36)</a>:</h4>
<p>Does a Rust's Hashmap own the actual value and when I <code>remove</code> it from the HashMap I get an Option&lt;V&gt; because the ownership is getting transferred?</p>



<a name="167930366"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167930366" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167930366">(Jun 12 2019 at 09:42)</a>:</h4>
<p>yes</p>



<a name="167930388"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167930388" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167930388">(Jun 12 2019 at 09:42)</a>:</h4>
<p>Same thing with a <code>Vec</code>. When you <code>pop</code> the last elemen, you get ownership back</p>



<a name="167933177"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167933177" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Daniel Papp <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167933177">(Jun 12 2019 at 10:25)</a>:</h4>
<p>Thanks!</p>



<a name="167935315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167935315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167935315">(Jun 12 2019 at 11:00)</a>:</h4>
<p>speaking of which, does someone know a nice and readable way to assert that we got a <code>None</code> back?<br>
I dislike <code>assert!(foo.remove(...).is_none());</code> because usually one expects the body of an <code>assert!</code> to not have important side-effects.</p>



<a name="167955560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167955560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167955560">(Jun 12 2019 at 15:04)</a>:</h4>
<p>The dumb answer is to move the side-effect out of the assert; is that good enough?</p>
<p>I've been bitten by putting important-to-the-functionality code in asserts in C and wondering why the production code fails, but at least in Rust you have to do out of your way to do that with <code>debug_assert</code></p>



<a name="167955641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167955641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167955641">(Jun 12 2019 at 15:04)</a>:</h4>
<p>Depending on what you are testing, you could just do <code>assert(foo.is_empty())</code></p>



<a name="167955693"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167955693" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167955693">(Jun 12 2019 at 15:05)</a>:</h4>
<p>And I think the context is relevant. I have no problems with side effects in an assert in a test.</p>



<a name="167957096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167957096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167957096">(Jun 12 2019 at 15:18)</a>:</h4>
<p>I don't understand why one would write <code>assert!(foo.remove(...).is_none())</code>. If it returns None and the assertion passes, then the map didn't contain the key to begin with and nothing was removed, so why can't this be written as <code>assert!(!foo.contains(...));</code>?</p>



<a name="167957318"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167957318" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167957318">(Jun 12 2019 at 15:20)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> I think the original example was meant to be <code>HashMap::insert</code></p>



<a name="167957343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167957343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167957343">(Jun 12 2019 at 15:21)</a>:</h4>
<p>Ah, that makes sense</p>



<a name="167957953"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167957953" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167957953">(Jun 12 2019 at 15:27)</a>:</h4>
<p>heh, you could do <code>assert!(map.insert(the_key_again).is_some())</code> to be really confusing</p>



<a name="167958486"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958486" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958486">(Jun 12 2019 at 15:32)</a>:</h4>
<p>Now I want a clippy lint against that because I've never seen it used and thus have concluded via anecdotal evidence that the pattern is probably a bug and the user meant <code>.is_none()</code></p>



<a name="167958550"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958550" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958550">(Jun 12 2019 at 15:32)</a>:</h4>
<p>actually</p>



<a name="167958564"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958564" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958564">(Jun 12 2019 at 15:32)</a>:</h4>
<p>the user should just have done <code>map[key] = value;</code> instead</p>



<a name="167958602"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958602" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958602">(Jun 12 2019 at 15:33)</a>:</h4>
<p>You can't do that :-)</p>



<a name="167958626"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958626" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958626">(Jun 12 2019 at 15:33)</a>:</h4>
<p><a href="https://stackoverflow.com/a/30414450/155423" target="_blank" title="https://stackoverflow.com/a/30414450/155423">How can I update a value in a mutable HashMap?</a></p>



<a name="167958726"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958726" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958726">(Jun 12 2019 at 15:34)</a>:</h4>
<p>damn</p>



<a name="167958734"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958734" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958734">(Jun 12 2019 at 15:34)</a>:</h4>
<p><span class="user-mention" data-user-id="124288">@oli</span> tangentially, I saw some SO comments from you recently back when you started Rust. It's really fun seeing the delta to today :-)</p>



<a name="167958764"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958764" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958764">(Jun 12 2019 at 15:34)</a>:</h4>
<p>hahah</p>



<a name="167958766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958766">(Jun 12 2019 at 15:34)</a>:</h4>
<p>yea</p>



<a name="167958815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167958815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167958815">(Jun 12 2019 at 15:35)</a>:</h4>
<p>it's probably visible just in my year of SO</p>



<a name="167961736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167961736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167961736">(Jun 12 2019 at 16:02)</a>:</h4>
<p>yeah sorry I meant <code>insert</code>. and I meant in real code, not tests, so side-effects in the <code>assert!</code> are bad.</p>



<a name="167961743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167961743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167961743">(Jun 12 2019 at 16:02)</a>:</h4>
<p>I also dont want to spend two lines on this</p>



<a name="167961789"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167961789" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167961789">(Jun 12 2019 at 16:03)</a>:</h4>
<p>for the other case ("assert <code>is_some</code>"), we have <code>unwrap</code>. So what is missing is a method that panics on <code>some</code> that can be nicely chained.</p>



<a name="167961920"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167961920" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167961920">(Jun 12 2019 at 16:03)</a>:</h4>
<p>or alternatively, a method on <code>bool</code> to assert it is <code>true</code> or so...<br>
<code>map.insert(...).is_none().assert()</code> ;)</p>



<a name="167965926"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167965926" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167965926">(Jun 12 2019 at 16:49)</a>:</h4>
<p>ahem <code>map.insert(...).is_none().assert!()</code></p>



<a name="167966241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167966241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167966241">(Jun 12 2019 at 16:53)</a>:</h4>
<p>Gonna drag the postfix macros bikeshed into this faffing about, eh</p>



<a name="167966323"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167966323" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167966323">(Jun 12 2019 at 16:54)</a>:</h4>
<p>So you want <code>Option::unwrap_none</code>; in parallel to <code>Result::unwrap_err</code>, almost.</p>



<a name="167966660"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167966660" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167966660">(Jun 12 2019 at 17:00)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> who puts <code>assert</code>s in the production code? Don't you trust yourself <span aria-label="wink" class="emoji emoji-1f609" role="img" title="wink">:wink:</span></p>



<a name="167966905"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167966905" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167966905">(Jun 12 2019 at 17:02)</a>:</h4>
<p>"Beware of bugs in my code. I have only proven it correct, not tested it."</p>



<a name="167966909"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/HashMap%3A%3Aremove%20return%20value/near/167966909" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/HashMap.3A.3Aremove.20return.20value.html#167966909">(Jun 12 2019 at 17:02)</a>:</h4>
<blockquote>
<p>So you want <code>Option::unwrap_none</code>; in parallel to <code>Result::unwrap_err</code>, almost.</p>
</blockquote>
<p>yes</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>